package model.dao;

import java.util.List;

import model.entity.Resposta;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 *
 * Resposta DAO
 *
 * @author Alex
 *
 */

@Repository(value="respostaDAO")
public class RespostaDAO implements IRespostaDAO {

	@Autowired
	private SessionFactory sessionFactory;

	/**
	 * Get Hibernate Session Factory
	 *
	 * @return SessionFactory - Hibernate Session Factory
	 */
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/**
	 * Set Hibernate Session Factory
	 *
	 * @param SessionFactory
	 *            - Hibernate Session Factory
	 */
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	/**
	 * Add Resposta
	 *
	 * @param Resposta
	 *            resposta
	 */
	@Override
	public void addResposta(Resposta resposta) {
		getSessionFactory().getCurrentSession().save(resposta);
	}

	/**
	 * Delete Resposta
	 *
	 * @param Resposta
	 *            resposta
	 */
	@Override
	public void deleteResposta(Resposta resposta) {
		getSessionFactory().getCurrentSession().delete(resposta);
	}

	/**
	 * Update Resposta
	 *
	 * @param Resposta
	 *            resposta
	 */
	@Override
	public void updateResposta(Resposta resposta) {
		getSessionFactory().getCurrentSession().update(resposta);
	}

	/**
	 * Get Resposta
	 *
	 * @param int Resposta Id
	 * @return Resposta
	 */
	@Override
	public Resposta getRespostaById(int id) {
		@SuppressWarnings("unchecked")
		List<Resposta> list = getSessionFactory().getCurrentSession()
				.createQuery("from Resposta where id=?")
				.setParameter(0, id).list();
		return (list.isEmpty()) ? null : list.get(0);
	}

	/**
	 * Get Resposta List
	 *
	 * @return List - Resposta list
	 */
	@Override
	public List<Resposta> getRespostas() {
		@SuppressWarnings("unchecked")
		List<Resposta> list = getSessionFactory().getCurrentSession()
				.createQuery("from Resposta").list();
		return list;
	}

}
